"use strict";
const express = require('express');
const router = express.Router();
const retUtil = require("../utils/commonUtils/retUtil");
const CONSTANT =  require("../const/CONST");
const designsVideosUtil = require("../utils/routerUtils/designsVideosUtil");
const tokenCheck = require('../middleware/tokenCheck');
router.use(tokenCheck);


/* GET home page. */
router.get('/', function(req, res, next) {
    res.render('index', { title: 'Express' });
});

/***
 * 获取视频模板列表
 * @author mk
 * @date 2018-5-30
 * 参数：token      必须   xxxxxx       入驻机构唯一标识
 * 参数：page_num   必须   0            页码, 从零开始
 * 参数：page_size  必须   12           页容量, 1, 99
 * 测试地址 : http://localhost:3014/v1/designs/videos/getDesignList?token=XXXXX&page_num=0&page_size=12
 */
function getDesignList(req, res, next) {
    let token = retUtil.stringFromReq(req, 'token');
    let pageNum = retUtil.numberFromReq(req,'page_num');
    let pageSize = retUtil.numberFromReq(req,'page_size');
    //初步判断参数的有效性
    if (!token || !pageSize || token.length <= 0 || pageNum < 0 || pageSize <= 0 || pageSize > 99){
        return retUtil.ret_err_json(CONSTANT.CODE.INVALID_PARAMS,res);
    }
    //获取视频模板列表
    designsVideosUtil.getDesignList(token,pageNum,pageSize,function (err,data) {
        if (err){
            console.log("routes/v1/design/videos.js  getDesignList err" + err);
            return retUtil.ret_err_json(err,res);
        }
        return retUtil.ret_succ_json(data,res);
    });
}
router.post('/getDesignList', getDesignList);
router.get('/getDesignList', getDesignList);


/***
 * 获取视频模板信息
 * @author mk
 * @date 2018-5-30
 * 参数：token      必须   xxxxxx       入驻机构唯一标识
 * 参数：design_id  必须   9998         视频模板唯一标识
 * 测试地址 : http://localhost:3014/v1/designs/videos/getDesignContent?token=XXXXX&design_id=9103
 */
function getDesignContent(req, res, next) {
    let token = retUtil.stringFromReq(req, 'token');
    let designId = retUtil.numberFromReq(req,'design_id');
    //初步判断参数的有效性
    if (!token || !designId || token.length <= 0 || designId <= 0){
        return retUtil.ret_err_json(CONSTANT.CODE.INVALID_PARAMS,res);
    }
    //获取视频模板信息
    designsVideosUtil.getDesignContent(token,designId,function (err,data) {
        if (err){
            console.log("routes/v1/design/videos.js  getDesignContent err" + err);
            return retUtil.ret_err_json(err,res);
        }
        return retUtil.ret_succ_json(data,res);
    });
}
router.post('/getDesignContent', getDesignContent);
router.get('/getDesignContent', getDesignContent);



module.exports = router;
